//获取cookie对象，格式化成对象形式
function getCookiesObj(){
    var cookies = {};
    if(document.cookie){
        var objs = document.cookie.split(';') || document.cookie;
        for(var i in objs){
            var index = objs[i].indexOf('=');
            var key = objs[i].substr(0,index);
            var value = objs[i].substr(index+1,objs[i].length);
            cookies[key] = value;
        }
    }
    return cookies;
}
//获取cookie
function get(key){
    return decodeURIComponent(getCookiesObj()[key]) || null;
}
//设置cookie,name:cookie名称,value:cookie值,opts:关于过期时间、路径等的一个对象
function set(name,value,opts){
    //opts: maxAge,path,domain,secure
    if(name && value){
        var cookie = encodeURIComponent(name) + '=' + encodeURIComponent(value);
        if(opts){
            if(opts.maxAge){
                cookie += ';max-age=' + opts.maxAge;
            }
            if(opts.path){
                cookie += ';max-age=' + opts.path;
            }
            if(opts.domain){
                cookie += ';max-age=' + opts.doamin;
            }
            if(opts.secure){
                cookie += ';max-age=' + opts.secure;
            }
        }
        document.cookie = cookie;
        return cookie;
    }else{
        return '';
    }
}
function remove(key){
    if(getCookiesObj()[key]){
        document.cookie = key + '=;max-age=0';
    }
}
function clear(){
    var cookies = getCookiesObj();
    for(var i in cookies){
        document.cookie = cookies[i] + '=;max-age=0';
    }
}

export default{
	clear:clear,
	set:set,
	get:get,
	remove:remove
}
